import Router from 'vue-router'
import Vue from 'vue'
import store from "../store/index"

Vue.use(Router)


const Login = (resolve => {
    import('../components/login/login').then((module) => {
        resolve(module)
    })
})

const Notes = (resolve => {
    import('../components/page/notes').then((module) => {
        resolve(module)
    })
})

const Home = (resolve => {
    import('../components/home').then((module) => {
        resolve(module)
    })
})

const SiteMap = (resolve => {
    import('../components/page/siteMap').then((module) => {
        resolve(module)
    })
})
const Task = (resolve => {
    import('../components/page/Task').then((module) => {
        resolve(module)
    })
})


const router = new Router({
    // mode: 'history',
    routes: [
        {
            path: "/",
            redirect: '/login',
        },
        {
            path: "/login",
            component: Login
        },
        {
            path: '/home',
            component: Home,
            children: [
                {
                    path: '/home',
                    component: SiteMap,
                    meta: {
                        requireAuth: true,
                    }
                },
                {
                    path: '/home/notes',
                    component: Notes,
                    meta: {
                        requireAuth: true,
                    }
                },
                {
                    path: '/home/task',
                    component: Task,
                    meta: {
                        requireAuth: true,
                    }
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        console.log(store.getters.userMessage)
        if (store.getters.userMessage) {
            next();
        } else {
            next({
                path: '/login',
                // query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
})
export default router
